package com.seeway.wms.utils.bindadapter

import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.geen.tuoke.R


object CustomBindAdapter {


    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView, url: String?) {
        Glide.with(view.context.applicationContext)
            .load(url.toString())
            .into(view)
    }

    @BindingAdapter("imageCenter")
    @JvmStatic
    fun loadCenterImage(view: ImageView?, imageUrl: String?) {
        if (TextUtils.isEmpty(imageUrl)) {
            return
        }
        Glide.with(view!!)
            .load(imageUrl)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerInside()
            .dontAnimate()
            .into(view)
    }

    @BindingAdapter(value = ["loadAvatar"])
    @JvmStatic
    fun loadAvatar(view: ImageView, url: String?) {
        Glide.with(view.context.applicationContext)
            .load(url.toString())
            .placeholder(R.mipmap.icon_avatar_place)
            .error(R.mipmap.icon_avatar_place)
            .into(view)
    }

    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView, url: String) {
        Glide.with(view.context.applicationContext)
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }


    @BindingAdapter(value = ["imageResName"])
    @JvmStatic
    fun imageResName(view: ImageView, resName: String) {
        view.setImageResource(
            view.context.resources.getIdentifier(
                resName.toLowerCase(),
                "drawable",
                view.context.packageName
            )
        )
    }

    @BindingAdapter(value = ["imageResId"])
    @JvmStatic
    fun imageResId(view: ImageView, @DrawableRes resId: Int) {
        if (resId == 0) {
            view.setImageDrawable(null)
        } else {
            view.setImageResource(resId)
        }

    }


    @BindingAdapter("onEditorActionDoneListener")
    @JvmStatic
    fun onEditorActionDoneListener(view: TextView, listener: OnEditorActionDoneListener?) {
        if (listener == null) {
            view.setOnEditorActionListener(null)
        } else {
            view.imeOptions = EditorInfo.IME_ACTION_DONE
            view.setOnEditorActionListener { v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    listener.onEditorActionDone(v)
                }
                false
            }
        }
    }

    public interface OnEditorActionDoneListener {
        fun onEditorActionDone(view: TextView)
    }
}